const form = document.querySelector("form");
const buttons = form.querySelectorAll("button");

// 创建配置实例
const instance = axios.create({
    baseURL: null,
    timeout: 1000,
    withCredentials: true,
    headers: {
        token: "abc123"
    }
});

buttons[0].addEventListener("click", () => {
    //axios.get(url[, config])
    axios.get('get', {
        params: {
            string: form.querySelector("input[name='string']").value,
            localDate: form.querySelector("input[name='localDate']").value
        },
        timeout: 1000
    })
        .then(response => {
            console.log(response.data);
            document.querySelector("#show").innerText = JSON.stringify(response.data);
        })
        .catch(error => console.log(error));

    // instance.get("get", {
    //     params: {
    //         string: form.querySelector("input[name='string']").value,
    //         localDate: form.querySelector("input[name='localDate']").value
    //     }
    // })
    //     .then(response => {
    //         console.log(response.data);
    //         document.querySelector("#show").innerText = JSON.stringify(response.data);
    //     })
    //     .catch(error => console.log(error));
})

buttons[1].addEventListener("click", () => {
    // 使用URLSearchParams API
    // const params = new URLSearchParams();
    // params.append("string", form.querySelector("input[name='string']").value);
    // params.append("localDate", form.querySelector("input[name='localDate']").value);

    // axios.post(url[, data[, config]])
    // axios.post('post',
    //     params,
    //     {
    //         withCredentials: true,
    //         headers: {
    //             token: "abc123"
    //         }
    //     })
    //     .then(response => {
    //         console.log(response.data);
    //         document.querySelector("#show").innerText = JSON.stringify(response.data);
    //     })
    //     .catch(error => console.log(error));

    // 使用qs库
    axios({
        method: 'post',
        url: 'post',
        data: Qs.stringify({
            string: form.querySelector("input[name='string']").value,
            localDate: form.querySelector("input[name='localDate']").value
        })
    })
        .then(response => {
            console.log(response.data);
            document.querySelector("#show").innerText = JSON.stringify(response.data);
        })
        .catch(error => console.log(error));
})

